package com.ruoyi.caipiao.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * @author wangcen
 * @date 2025/1/3 21:44
 */
@Slf4j
public class Pl3Test8888 {
    RestTemplate restTemplate = new RestTemplate();
    static String pl5History = "D:\\pl5\\history\\history.txt";
    static String URL = "https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=350133&provinceId=0&pageSize=100&isVerify=1&pageNo=";
    static String URL2 = "https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=350133&provinceId=0&pageSize=1&isVerify=1&pageNo=";

    static String ycFile = "D:\\pl5\\";

    static LinkedList<String> HISTORY_LIST;
    static LinkedList<String> list;
    static String yyyyMMdd;

    @Before
    public void before() {
        yyyyMMdd = "20250210";//DateUtil.format(new Date(), "yyyyMMdd");

        ycFile = ycFile + yyyyMMdd + "_预测.txt";


        HISTORY_LIST = (LinkedList<String>) MyFileUtil.readFileAsList(new File(pl5History));

        list = this.getSubLinkedList();
    }

    @Test
    public void getYcNum() {

        this.method01();
        System.out.println("method01 finished");
//        this.method02();
        System.out.println("method02 finished");
        this.method03();
        System.out.println("method03 finished");
//        this.method04();
        System.out.println("method04 finished");
        this.method05();
        System.out.println("method05 finished");
//        this.method06();
        System.out.println("method06 finished");
//        this.method07();
        System.out.println("method07 finished");
        List<String> list = MyFileUtil.readFileAsList(new File(ycFile));
        Map<String, List<String>> map = new HashMap<>();
        for (String s : list) {
            if (StringUtils.isBlank(s)) {
                continue;
            }
            String[] split = s.split("_");
            List<String> p = map.get(split[0]);
            if (p == null) {
                ArrayList<String> ss = new ArrayList<>();
                ss.add(split[1]);
                map.put(split[0], ss);
            } else {
                p.add(split[1]);
            }
        }
        MyFileUtil.writeContentToFile(ycFile, "预测排除：" + "\n", true);
        for (Map.Entry<String, List<String>> entry : map.entrySet()) {
            MyFileUtil.writeContentToFile(ycFile, entry.getKey() + "----------->" + new TreeSet<>(entry.getValue()) + "\n", true);
        }

    }

    /**
     * 09
     * 交集获取得到一注号码， 这一注号码一个号码都不中， 则各位置可以排除一个号码
     */
    @Test
    public void method09() {

        Set<String> nums = Pl5NumUtil.getNums(800);
        for (;;) {
            nums.retainAll(Pl5NumUtil.getNums(800));
            if(nums.size() == 2) {
                log.info("得到一注号码：{}", nums);
                break;
            } else if(nums.isEmpty()) {
                nums = Pl5NumUtil.getNums(800);
            }
        }
    }


    /**
     * 方法8
     * 各位置的前30期，分成3个10期， 号码交集合作为排除
     */
    @Test
    public void method08() {
        int retainSize = 5;
        int size = 3 * retainSize;
        List<String> newList = this.getSubList(size);

        Set<String> p0Set_1 = getPositionSet(newList, 0, 0, retainSize);
        Set<String> p0Set_2 = getPositionSet(newList, 1, 0, retainSize);
        Set<String> p0Set_3 = getPositionSet(newList, 2, 0, retainSize);
        p0Set_1.retainAll(p0Set_2);
        p0Set_1.retainAll(p0Set_3);
        System.out.println("p0Set = " + p0Set_1);

        Set<String> p1Set_1 = getPositionSet(newList, 0, 1, retainSize);
        Set<String> p1Set_2 = getPositionSet(newList, 1, 1, retainSize);
        Set<String> p1Set_3 = getPositionSet(newList, 2, 1, retainSize);
        p1Set_1.retainAll(p1Set_2);
        p1Set_1.retainAll(p1Set_3);
        System.out.println("p1Set = " + p1Set_1);

        Set<String> p2Set_1 = getPositionSet(newList, 0, 2, retainSize);
        Set<String> p2Set_2 = getPositionSet(newList, 1, 2, retainSize);
        Set<String> p2Set_3 = getPositionSet(newList, 2, 2, retainSize);
        p2Set_1.retainAll(p2Set_2);
        p2Set_1.retainAll(p2Set_3);
        System.out.println("p2Set = " + p2Set_1);


    }

    public Set<String> getPositionSet(List<String> list, int index, int position, int retainSize) {
        Set<String> set = new TreeSet<>();
        int start = index * retainSize;
        int end = (index + 1) * retainSize;
        for (int i = start; i < end; i++) {
            String s = list.get(i);
            String code = s.substring(9);

            set.add(code.substring(position, position + 1));
        }
        return set;
    }

    /**
     * 方法7
     * 把各位置 上4次开出号码相加的平均值取整 后 紧接着开出的号码做统计，最近出现的，作为排除
     */
    @Test
    public void method07() {
        List<String> newList = this.getSubList(60);//传5的倍数
        String newKjCode0 = list.getFirst().substring(9);
        String code00 = newKjCode0.substring(0, 1);
        String code01 = newKjCode0.substring(1, 2);
        String code02 = newKjCode0.substring(2, 3);
        String code03 = newKjCode0.substring(3, 4);
        String code04 = newKjCode0.substring(4, 5);

        String newKjCode1 = list.get(1).substring(9);
        String code10 = newKjCode1.substring(0, 1);
        String code11 = newKjCode1.substring(1, 2);
        String code12 = newKjCode1.substring(2, 3);
        String code13 = newKjCode1.substring(3, 4);
        String code14 = newKjCode1.substring(4, 5);

        String newKjCode2 = list.get(2).substring(9);
        String code20 = newKjCode2.substring(0, 1);
        String code21 = newKjCode2.substring(1, 2);
        String code22 = newKjCode2.substring(2, 3);
        String code23 = newKjCode2.substring(3, 4);
        String code24 = newKjCode2.substring(4, 5);

        String newKjCode3 = list.get(3).substring(9);
        String code30 = newKjCode3.substring(0, 1);
        String code31 = newKjCode3.substring(1, 2);
        String code32 = newKjCode3.substring(2, 3);
        String code33 = newKjCode3.substring(3, 4);
        String code34 = newKjCode3.substring(4, 5);

        String sum0 = new BigDecimal(code00).add(new BigDecimal(code10)).add(new BigDecimal(code20)).add(new BigDecimal(code30)).divide(new BigDecimal("4"), RoundingMode.UP).toString();
        String sum1 = new BigDecimal(code01).add(new BigDecimal(code11)).add(new BigDecimal(code21)).add(new BigDecimal(code31)).divide(new BigDecimal("4"), RoundingMode.UP).toString();
        String sum2 = new BigDecimal(code02).add(new BigDecimal(code12)).add(new BigDecimal(code22)).add(new BigDecimal(code32)).divide(new BigDecimal("4"), RoundingMode.UP).toString();
        String sum3 = new BigDecimal(code03).add(new BigDecimal(code13)).add(new BigDecimal(code23)).add(new BigDecimal(code33)).divide(new BigDecimal("4"), RoundingMode.UP).toString();
        String sum4 = new BigDecimal(code04).add(new BigDecimal(code14)).add(new BigDecimal(code24)).add(new BigDecimal(code34)).divide(new BigDecimal("4"), RoundingMode.UP).toString();

        Map<String, Integer> mapP0 = new TreeMap<>();
        Map<String, Integer> mapP1 = new TreeMap<>();
        Map<String, Integer> mapP2 = new TreeMap<>();
        Map<String, Integer> mapP3 = new TreeMap<>();
        Map<String, Integer> mapP4 = new TreeMap<>();
        for (int i = newList.size() - 1; i > 0; i = i - 5) {
            String preNum0 = newList.get(i).substring(9);  //上一期的号码
            String preNum1 = newList.get(i - 1).substring(9);  //上两期的号码
            String preNum2 = newList.get(i - 2).substring(9);   //紧接着开出的号码
            String preNum3 = newList.get(i - 3).substring(9);   //紧接着开出的号码
            String kcNum = newList.get(i - 4).substring(9);   //紧接着开出的号码

            putSum4AvgNumCount(mapP0, 0, preNum0, preNum1, preNum2, preNum3, kcNum);
            putSum4AvgNumCount(mapP1, 1, preNum0, preNum1, preNum2, preNum3, kcNum);
            putSum4AvgNumCount(mapP2, 2, preNum0, preNum1, preNum2, preNum3, kcNum);
            putSum4AvgNumCount(mapP3, 3, preNum0, preNum1, preNum2, preNum3, kcNum);
            putSum4AvgNumCount(mapP4, 4, preNum0, preNum1, preNum2, preNum3, kcNum);
        }
        System.out.println("------p0------------------");
        for (Map.Entry<String, Integer> entry : mapP0.entrySet()) {
            if (entry.getKey().startsWith(sum0)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p0_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
        System.out.println("------p1------------------");
        for (Map.Entry<String, Integer> entry : mapP1.entrySet()) {
            if (entry.getKey().startsWith(sum1)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p1_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);

            }
        }
        System.out.println("------p2------------------");
        for (Map.Entry<String, Integer> entry : mapP2.entrySet()) {
            if (entry.getKey().startsWith(sum2)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p2_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
        System.out.println("------p3------------------");
        for (Map.Entry<String, Integer> entry : mapP3.entrySet()) {
            if (entry.getKey().startsWith(sum3)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p3_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);

            }
        }
        System.out.println("------p4------------------");
        for (Map.Entry<String, Integer> entry : mapP4.entrySet()) {
            if (entry.getKey().startsWith(sum4)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p4_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
    }

    private void putSum4AvgNumCount(Map<String, Integer> map, int i, String preNum0, String preNum1, String preNum2, String preNum3, String kcNum) {
        String p0 = preNum0.substring(i, i + 1);
        String p1 = preNum1.substring(i, i + 1);
        String p2 = preNum2.substring(i, i + 1);
        String p3 = preNum3.substring(i, i + 1);
        int sum = Integer.parseInt(p0) + Integer.parseInt(p1) + Integer.parseInt(p2) + Integer.parseInt(p3);
        String avg = new BigDecimal(sum + "").divide(new BigDecimal("4"), RoundingMode.UP).toPlainString();

        String k0 = kcNum.substring(i, i + 1);
        map.merge(avg + "-" + k0, 1, Integer::sum);
    }

    /**
     * 方法6
     * 把各位置 上3次开出号码相加 后 紧接着开出的号码做统计，最近出现的，作为排除
     */
    @Test
    public void method06() {
        List<String> newList = this.getSubList(80);
        String newKjCode0 = list.getFirst().substring(9);
        String code00 = newKjCode0.substring(0, 1);
        String code01 = newKjCode0.substring(1, 2);
        String code02 = newKjCode0.substring(2, 3);
        String code03 = newKjCode0.substring(3, 4);
        String code04 = newKjCode0.substring(4, 5);

        String newKjCode1 = list.get(1).substring(9);
        String code10 = newKjCode1.substring(0, 1);
        String code11 = newKjCode1.substring(1, 2);
        String code12 = newKjCode1.substring(2, 3);
        String code13 = newKjCode1.substring(3, 4);
        String code14 = newKjCode1.substring(4, 5);

        String newKjCode2 = list.get(2).substring(9);
        String code20 = newKjCode2.substring(0, 1);
        String code21 = newKjCode2.substring(1, 2);
        String code22 = newKjCode2.substring(2, 3);
        String code23 = newKjCode2.substring(3, 4);
        String code24 = newKjCode2.substring(4, 5);

        String sum0 = new BigDecimal(code00).add(new BigDecimal(code10)).add(new BigDecimal(code20)).toString();
        String sum1 = new BigDecimal(code01).add(new BigDecimal(code11)).add(new BigDecimal(code21)).toString();
        String sum2 = new BigDecimal(code02).add(new BigDecimal(code12)).add(new BigDecimal(code22)).toString();
        String sum3 = new BigDecimal(code03).add(new BigDecimal(code13)).add(new BigDecimal(code23)).toString();
        String sum4 = new BigDecimal(code04).add(new BigDecimal(code14)).add(new BigDecimal(code24)).toString();

        Map<String, Integer> mapP0 = new TreeMap<>();
        Map<String, Integer> mapP1 = new TreeMap<>();
        Map<String, Integer> mapP2 = new TreeMap<>();
        Map<String, Integer> mapP3 = new TreeMap<>();
        Map<String, Integer> mapP4 = new TreeMap<>();
        for (int i = newList.size() - 1; i > 0; i = i - 4) {
            String preNum0 = newList.get(i).substring(9);  //上一期的号码
            String preNum1 = newList.get(i - 1).substring(9);  //上两期的号码
            String preNum2 = newList.get(i - 2).substring(9);   //紧接着开出的号码
            String kcNum = newList.get(i - 3).substring(9);   //紧接着开出的号码
            System.out.println(i + "--->");
            putSum3NumCount(mapP0, 0, preNum0, preNum1, preNum2, kcNum);
            putSum3NumCount(mapP1, 1, preNum0, preNum1, preNum2, kcNum);
            putSum3NumCount(mapP2, 2, preNum0, preNum1, preNum2, kcNum);
            putSum3NumCount(mapP3, 3, preNum0, preNum1, preNum2, kcNum);
            putSum3NumCount(mapP4, 4, preNum0, preNum1, preNum2, kcNum);
        }
        System.out.println("------p0------------------");
        for (Map.Entry<String, Integer> entry : mapP0.entrySet()) {
            if (entry.getKey().startsWith(sum0)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p0_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);

            }
        }
        System.out.println("------p1------------------");
        for (Map.Entry<String, Integer> entry : mapP1.entrySet()) {
            if (entry.getKey().startsWith(sum1)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p1_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
        System.out.println("------p2------------------");
        for (Map.Entry<String, Integer> entry : mapP2.entrySet()) {
            if (entry.getKey().startsWith(sum2)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p2_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
        System.out.println("------p3------------------");
        for (Map.Entry<String, Integer> entry : mapP3.entrySet()) {
            if (entry.getKey().startsWith(sum3)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p3_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
        System.out.println("------p4------------------");
        for (Map.Entry<String, Integer> entry : mapP4.entrySet()) {
            if (entry.getKey().startsWith(sum4)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p4_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
    }


    private void putSum3NumCount(Map<String, Integer> map, int i, String preNum0, String preNum1, String preNum2, String kcNum) {
        String p0 = preNum0.substring(i, i + 1);
        String p1 = preNum1.substring(i, i + 1);
        String p2 = preNum2.substring(i, i + 1);
        int sum = Integer.parseInt(p0) + Integer.parseInt(p1) + Integer.parseInt(p2);

        String k0 = kcNum.substring(i, i + 1);
        map.merge(sum + "-" + k0, 1, Integer::sum);
    }

    /**
     * 方法5
     * 把各位置 上两次开出号码相取模 后 紧接着开出的号码做统计，最近出现的，作为排除
     */
    @Test
    public void method05() {
        List<String> newList = this.getSubList(60);
//        ArrayList<String> newList = new ArrayList<>(list.subList(0, 30));//传3的倍数
        String newKjCode0 = list.getFirst().substring(9);
        String code00 = newKjCode0.substring(0, 1);
        String code01 = newKjCode0.substring(1, 2);
        String code02 = newKjCode0.substring(2, 3);
        String code03 = newKjCode0.substring(3, 4);
        String code04 = newKjCode0.substring(4, 5);

        String newKjCode1 = list.get(1).substring(9);
        String code10 = newKjCode1.substring(0, 1);
        String code11 = newKjCode1.substring(1, 2);
        String code12 = newKjCode1.substring(2, 3);
        String code13 = newKjCode1.substring(3, 4);
        String code14 = newKjCode1.substring(4, 5);

        String sum0 = code10.equals("0") ? code00 : new BigDecimal(code00).remainder(new BigDecimal(code10)).abs().toString();
        String sum1 = code11.equals("0") ? code01 : new BigDecimal(code01).remainder(new BigDecimal(code11)).abs().toString();
        String sum2 = code12.equals("0") ? code02 : new BigDecimal(code02).remainder(new BigDecimal(code12)).abs().toString();
        String sum3 = code13.equals("0") ? code03 : new BigDecimal(code03).remainder(new BigDecimal(code13)).abs().toString();
        String sum4 = code14.equals("0") ? code04 : new BigDecimal(code04).remainder(new BigDecimal(code14)).abs().toString();

        Map<String, Integer> mapP0 = new TreeMap<>();
        Map<String, Integer> mapP1 = new TreeMap<>();
        Map<String, Integer> mapP2 = new TreeMap<>();
        Map<String, Integer> mapP3 = new TreeMap<>();
        Map<String, Integer> mapP4 = new TreeMap<>();
        for (int i = newList.size() - 1; i > 0; i = i - 3) {
            String preNum0 = newList.get(i).substring(9);  //上一期的号码
            String preNum1 = newList.get(i - 1).substring(9);  //上两期的号码
            String kcNum = newList.get(i - 2).substring(9);   //紧接着开出的号码

            putRemaNumCount(mapP0, 0, preNum0, preNum1, kcNum);
            putRemaNumCount(mapP1, 1, preNum0, preNum1, kcNum);
            putRemaNumCount(mapP2, 2, preNum0, preNum1, kcNum);
            putRemaNumCount(mapP3, 3, preNum0, preNum1, kcNum);
            putRemaNumCount(mapP4, 4, preNum0, preNum1, kcNum);
        }
        System.out.println("------p0------------------");
        for (Map.Entry<String, Integer> entry : mapP0.entrySet()) {
            if (entry.getKey().startsWith(sum0)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p0_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
        System.out.println("------p1------------------");
        for (Map.Entry<String, Integer> entry : mapP1.entrySet()) {
            if (entry.getKey().startsWith(sum1)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p1_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);

            }
        }
        System.out.println("------p2------------------");
        for (Map.Entry<String, Integer> entry : mapP2.entrySet()) {
            if (entry.getKey().startsWith(sum2)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p2_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
        System.out.println("------p3------------------");
        for (Map.Entry<String, Integer> entry : mapP3.entrySet()) {
            if (entry.getKey().startsWith(sum3)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p3_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
        System.out.println("------p4------------------");
        for (Map.Entry<String, Integer> entry : mapP4.entrySet()) {
            if (entry.getKey().startsWith(sum4)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p4_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
    }

    private void putRemaNumCount(Map<String, Integer> map, int i, String preNum0, String preNum1, String kcNum) {
        String p0 = preNum0.substring(i, i + 1);
        String p1 = preNum1.substring(i, i + 1);
        String sum = p1.equals("0") ? p0 : new BigDecimal(p0).remainder(new BigDecimal(p1)).abs().toString();

        String k0 = kcNum.substring(i, i + 1);
        map.merge(sum + "-" + k0, 1, Integer::sum);
    }

    /**
     * 方法4
     * 把各位置 上两次开出号码相乘值 后 紧接着开出的号码做统计，最近出现的，作为排除
     */
    @Test
    public void method04() {
        List<String> newList = this.getSubList(100);
        String newKjCode0 = list.getFirst().substring(9);
        String code00 = newKjCode0.substring(0, 1);
        String code01 = newKjCode0.substring(1, 2);
        String code02 = newKjCode0.substring(2, 3);
        String code03 = newKjCode0.substring(3, 4);
        String code04 = newKjCode0.substring(4, 5);

        String newKjCode1 = list.get(1).substring(9);
        String code10 = newKjCode1.substring(0, 1);
        String code11 = newKjCode1.substring(1, 2);
        String code12 = newKjCode1.substring(2, 3);
        String code13 = newKjCode1.substring(3, 4);
        String code14 = newKjCode1.substring(4, 5);

        String sum0 = new BigDecimal(code00).multiply(new BigDecimal(code10)).abs().toString();
        String sum1 = new BigDecimal(code01).multiply(new BigDecimal(code11)).abs().toString();
        String sum2 = new BigDecimal(code02).multiply(new BigDecimal(code12)).abs().toString();
        String sum3 = new BigDecimal(code03).multiply(new BigDecimal(code13)).abs().toString();
        String sum4 = new BigDecimal(code04).multiply(new BigDecimal(code14)).abs().toString();

        Map<String, Integer> mapP0 = new TreeMap<>();
        Map<String, Integer> mapP1 = new TreeMap<>();
        Map<String, Integer> mapP2 = new TreeMap<>();
        Map<String, Integer> mapP3 = new TreeMap<>();
        Map<String, Integer> mapP4 = new TreeMap<>();
        for (int i = newList.size() - 1; i > 0; i = i - 3) {
            String preNum0 = newList.get(i).substring(9);  //上一期的号码
            String preNum1 = newList.get(i - 1).substring(9);  //上两期的号码
            String kcNum = newList.get(i - 2).substring(9);   //紧接着开出的号码

            putMultiNumCount(mapP0, 0, preNum0, preNum1, kcNum);
            putMultiNumCount(mapP1, 1, preNum0, preNum1, kcNum);
            putMultiNumCount(mapP2, 2, preNum0, preNum1, kcNum);
            putMultiNumCount(mapP3, 3, preNum0, preNum1, kcNum);
            putMultiNumCount(mapP4, 4, preNum0, preNum1, kcNum);
        }
        System.out.println("------p0------------------");
        for (Map.Entry<String, Integer> entry : mapP0.entrySet()) {
            if (entry.getKey().startsWith(sum0)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p0_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
        System.out.println("------p1------------------");
        for (Map.Entry<String, Integer> entry : mapP1.entrySet()) {
            if (entry.getKey().startsWith(sum1)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p1_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
        System.out.println("------p2------------------");
        for (Map.Entry<String, Integer> entry : mapP2.entrySet()) {
            if (entry.getKey().startsWith(sum2)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p2_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
        System.out.println("------p3------------------");
        for (Map.Entry<String, Integer> entry : mapP3.entrySet()) {
            if (entry.getKey().startsWith(sum3)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p3_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
        System.out.println("------p4------------------");
        for (Map.Entry<String, Integer> entry : mapP4.entrySet()) {
            if (entry.getKey().startsWith(sum4)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p4_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
    }

    private void putMultiNumCount(Map<String, Integer> map, int i, String preNum0, String preNum1, String kcNum) {
        String p0 = preNum0.substring(i, i + 1);
        String p1 = preNum1.substring(i, i + 1);
        String sum = new BigDecimal(p0).multiply(new BigDecimal(p1)).abs().toString();

        String k0 = kcNum.substring(i, i + 1);
        map.merge(sum + "-" + k0, 1, Integer::sum);
    }

    /**
     * 方法3
     * 把各位置 上两次开出号码差值绝对值 后 紧接着开出的号码做统计，最近出现的，作为排除
     */
    @Test
    public void method03() {
        List<String> newList = this.getSubList(30);
//        ArrayList<String> newList = new ArrayList<>(list.subList(0, 30));//传3的倍数
        String newKjCode0 = list.getFirst().substring(9);
        String code00 = newKjCode0.substring(0, 1);
        String code01 = newKjCode0.substring(1, 2);
        String code02 = newKjCode0.substring(2, 3);
        String code03 = newKjCode0.substring(3, 4);
        String code04 = newKjCode0.substring(4, 5);

        String newKjCode1 = list.get(1).substring(9);
        String code10 = newKjCode1.substring(0, 1);
        String code11 = newKjCode1.substring(1, 2);
        String code12 = newKjCode1.substring(2, 3);
        String code13 = newKjCode1.substring(3, 4);
        String code14 = newKjCode1.substring(4, 5);

        String sum0 = new BigDecimal(code00).subtract(new BigDecimal(code10)).abs().toString();
        String sum1 = new BigDecimal(code01).subtract(new BigDecimal(code11)).abs().toString();
        String sum2 = new BigDecimal(code02).subtract(new BigDecimal(code12)).abs().toString();
        String sum3 = new BigDecimal(code03).subtract(new BigDecimal(code13)).abs().toString();
        String sum4 = new BigDecimal(code04).subtract(new BigDecimal(code14)).abs().toString();

        Map<String, Integer> mapP0 = new TreeMap<>();
        Map<String, Integer> mapP1 = new TreeMap<>();
        Map<String, Integer> mapP2 = new TreeMap<>();
        Map<String, Integer> mapP3 = new TreeMap<>();
        Map<String, Integer> mapP4 = new TreeMap<>();
        for (int i = newList.size() - 1; i > 0; i = i - 3) {
            String preNum0 = newList.get(i).substring(9);  //上一期的号码
            String preNum1 = newList.get(i - 1).substring(9);  //上两期的号码
            String kcNum = newList.get(i - 2).substring(9);   //紧接着开出的号码

            putSubNumCount(mapP0, 0, preNum0, preNum1, kcNum);
            putSubNumCount(mapP1, 1, preNum0, preNum1, kcNum);
            putSubNumCount(mapP2, 2, preNum0, preNum1, kcNum);
            putSubNumCount(mapP3, 3, preNum0, preNum1, kcNum);
            putSubNumCount(mapP4, 4, preNum0, preNum1, kcNum);
        }
        System.out.println("------p0------------------");
        for (Map.Entry<String, Integer> entry : mapP0.entrySet()) {
            if (entry.getKey().startsWith(sum0)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p0_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
        System.out.println("------p1------------------");
        for (Map.Entry<String, Integer> entry : mapP1.entrySet()) {
            if (entry.getKey().startsWith(sum1)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p1_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
        System.out.println("------p2------------------");
        for (Map.Entry<String, Integer> entry : mapP2.entrySet()) {
            if (entry.getKey().startsWith(sum2)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p2_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
        System.out.println("------p3------------------");
        for (Map.Entry<String, Integer> entry : mapP3.entrySet()) {
            if (entry.getKey().startsWith(sum3)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p3_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
        System.out.println("------p4------------------");
        for (Map.Entry<String, Integer> entry : mapP4.entrySet()) {
            if (entry.getKey().startsWith(sum4)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p4_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
    }

    private void putSubNumCount(Map<String, Integer> map, int i, String preNum0, String preNum1, String kcNum) {
        String p0 = preNum0.substring(i, i + 1);
        String p1 = preNum1.substring(i, i + 1);
        String sum = new BigDecimal(p0).subtract(new BigDecimal(p1)).abs().toString();

        String k0 = kcNum.substring(i, i + 1);
        map.merge(sum + "-" + k0, 1, Integer::sum);
    }

    /**
     * 方法2
     * 把各位置 上两次开出号码和值后紧接着开出的号码做统计，最近出现的，作为排除
     */
    @Test
    public void method02() {
        List<String> newList = this.getSubList(30);
        String newKjCode0 = list.getFirst().substring(9);
        String code00 = newKjCode0.substring(0, 1);
        String code01 = newKjCode0.substring(1, 2);
        String code02 = newKjCode0.substring(2, 3);
        String code03 = newKjCode0.substring(3, 4);
        String code04 = newKjCode0.substring(4, 5);

        String newKjCode1 = list.get(1).substring(9);
        String code10 = newKjCode1.substring(0, 1);
        String code11 = newKjCode1.substring(1, 2);
        String code12 = newKjCode1.substring(2, 3);
        String code13 = newKjCode1.substring(3, 4);
        String code14 = newKjCode1.substring(4, 5);

        int sum0 = Integer.parseInt(code00) + Integer.parseInt(code10);
        int sum1 = Integer.parseInt(code01) + Integer.parseInt(code11);
        int sum2 = Integer.parseInt(code02) + Integer.parseInt(code12);
        int sum3 = Integer.parseInt(code03) + Integer.parseInt(code13);
        int sum4 = Integer.parseInt(code04) + Integer.parseInt(code14);


        Map<String, Integer> mapP0 = new TreeMap<>();
        Map<String, Integer> mapP1 = new TreeMap<>();
        Map<String, Integer> mapP2 = new TreeMap<>();
        Map<String, Integer> mapP3 = new TreeMap<>();
        Map<String, Integer> mapP4 = new TreeMap<>();
        for (int i = newList.size() - 1; i > 0; i = i - 3) {
            String preNum0 = newList.get(i).substring(9);  //上一期的号码
            String preNum1 = newList.get(i - 1).substring(9);  //上两期的号码
            String kcNum = newList.get(i - 2).substring(9);   //紧接着开出的号码

            putSumNumCount(mapP0, 0, preNum0, preNum1, kcNum);
            putSumNumCount(mapP1, 1, preNum0, preNum1, kcNum);
            putSumNumCount(mapP2, 2, preNum0, preNum1, kcNum);
            putSumNumCount(mapP3, 3, preNum0, preNum1, kcNum);
            putSumNumCount(mapP4, 4, preNum0, preNum1, kcNum);
        }
        System.out.println("------p0------------------");
        for (Map.Entry<String, Integer> entry : mapP0.entrySet()) {
            if (entry.getKey().startsWith(Integer.toString(sum0))) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p0_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
        System.out.println("------p1------------------");
        for (Map.Entry<String, Integer> entry : mapP1.entrySet()) {
            if (entry.getKey().startsWith(Integer.toString(sum1))) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p1_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
        System.out.println("------p2------------------");
        for (Map.Entry<String, Integer> entry : mapP2.entrySet()) {
            if (entry.getKey().startsWith(Integer.toString(sum2))) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p2_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
        System.out.println("------p3------------------");
        for (Map.Entry<String, Integer> entry : mapP3.entrySet()) {
            if (entry.getKey().startsWith(Integer.toString(sum3))) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p3_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
        System.out.println("------p4------------------");
        for (Map.Entry<String, Integer> entry : mapP4.entrySet()) {
            if (entry.getKey().startsWith(Integer.toString(sum4))) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p4_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
    }

    private void putSumNumCount(Map<String, Integer> map, int i, String preNum0, String preNum1, String kcNum) {
        String p0 = preNum0.substring(i, i + 1);
        String p1 = preNum1.substring(i, i + 1);
        int sum = Integer.parseInt(p0) + Integer.parseInt(p1);

        String k0 = kcNum.substring(i, i + 1);
        map.merge(sum + "-" + k0, 1, Integer::sum);
    }

    /**
     * 方法1
     * 把各位置 上次开出号码后紧接着开出的号码做统计，最近出现的，作为排除
     */
    @Test
    public void method01() {

        List<String> newList = this.getSubList(40);
        String newKjCode = list.getFirst().substring(9);
        String code0 = newKjCode.substring(0, 1);
        String code1 = newKjCode.substring(1, 2);
        String code2 = newKjCode.substring(2, 3);
        String code3 = newKjCode.substring(3, 4);
        String code4 = newKjCode.substring(4, 5);

        Map<String, Integer> mapP0 = new TreeMap<>();
        Map<String, Integer> mapP1 = new TreeMap<>();
        Map<String, Integer> mapP2 = new TreeMap<>();
        Map<String, Integer> mapP3 = new TreeMap<>();
        Map<String, Integer> mapP4 = new TreeMap<>();
        for (int i = newList.size() - 1; i > 0; i = i - 2) {
            String preNum = newList.get(i).substring(9);  //上一个号码
            String kcNum = newList.get(i - 1).substring(9);   //紧接着开出的号码

            putNumCount(mapP0, 0, preNum, kcNum);
            putNumCount(mapP1, 1, preNum, kcNum);
            putNumCount(mapP2, 2, preNum, kcNum);
            putNumCount(mapP3, 3, preNum, kcNum);
            putNumCount(mapP4, 4, preNum, kcNum);
        }
        System.out.println("------p0------------------");
        for (Map.Entry<String, Integer> entry : mapP0.entrySet()) {
            if (entry.getKey().startsWith(code0)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p0_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
        System.out.println("------p1------------------");
        for (Map.Entry<String, Integer> entry : mapP1.entrySet()) {
            if (entry.getKey().startsWith(code1)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p1_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
        System.out.println("------p2------------------");
        for (Map.Entry<String, Integer> entry : mapP2.entrySet()) {
            if (entry.getKey().startsWith(code2)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p2_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
        System.out.println("------p3------------------");
        for (Map.Entry<String, Integer> entry : mapP3.entrySet()) {
            if (entry.getKey().startsWith(code3)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p3_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
        System.out.println("------p4------------------");
        for (Map.Entry<String, Integer> entry : mapP4.entrySet()) {
            if (entry.getKey().startsWith(code4)) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
                MyFileUtil.writeContentToFile(ycFile, "p4_" + entry.getKey().substring(entry.getKey().indexOf("-") + 1) + "\n", true);
            }
        }
    }

    /**
     * 统计某一位置，开出某号码后，下一次开出另一号码的次数
     * 比如位置0， 上一次开出 8， 下一次开出 9， 则记录 89的次数+1；
     *
     * @param map
     * @param i      比如位置0
     * @param preNum 上一次开出 8
     * @param kcNum  下一次开出 9
     */
    private void putNumCount(Map<String, Integer> map, Integer i, String preNum, String kcNum) {
        String p0 = preNum.substring(i, i + 1);
        String k0 = kcNum.substring(i, i + 1);
        map.merge(p0 + "-" + k0, 1, Integer::sum);
    }


    /**
     * 采集历史
     */
    @Test
    public void updateHistory() {
        LinkedList<String> list = (LinkedList<String>) MyFileUtil.readFileAsList(new File(pl5History));
        if (!list.isEmpty()) {
            String url = URL2 + 1;
            List<String> pl5Num = getPl5Num(url);
            if (!pl5Num.isEmpty()) {
                if (list.contains(pl5Num.get(0))) {
                    return;
                }
                for (int i = pl5Num.size() - 1; i >= 0; i--) {
                    list.addFirst(pl5Num.get(i));
                }
                StringBuilder sb = new StringBuilder();
                for (String str : pl5Num) {
                    sb.append(str).append("\n");
                }
                new File(pl5History).delete();
                MyFileUtil.writeContentToFile(pl5History, sb.toString(), true);
            }
        } else {
            for (int i = 1; i < 500; i++) {
                String url = URL + i;
                List<String> pl5Num = getPl5Num(url);
                StringBuilder sb = new StringBuilder();
                for (String str : pl5Num) {
                    sb.append(str).append("\n");
                }
                MyFileUtil.writeContentToFile(pl5History, sb.toString(), true);
            }
        }
    }


    private List<String> getSubList(Integer size) {
        DateTime cur = DateUtil.parse(yyyyMMdd, "yyyyMMdd");
        String yd = DateUtil.format(DateUtil.offsetDay(cur, -1), "yyyyMMdd");
        for (int i = 0; i < HISTORY_LIST.size(); i++) {
            if (yd.equals(HISTORY_LIST.get(i).substring(0, 8))) {
                return new ArrayList<>(HISTORY_LIST.subList(i, size + i));
            }
        }
        return new ArrayList<>();
    }

    private LinkedList<String> getSubLinkedList() {
        DateTime cur = DateUtil.parse(yyyyMMdd, "yyyyMMdd");
        String yd = DateUtil.format(DateUtil.offsetDay(cur, -1), "yyyyMMdd");
        for (int i = 0; i < HISTORY_LIST.size(); i++) {
            if (yd.equals(HISTORY_LIST.get(i).substring(0, 8))) {
                return new LinkedList<>(HISTORY_LIST.subList(i, HISTORY_LIST.size() - i));
            }
        }
        return new LinkedList<>();
    }

    private LinkedList<String> getPl5Num(String url) {
        LinkedList<String> list = new LinkedList<>();
        String forObject = restTemplate.getForObject(url, String.class);
        JSONObject entries = JSONUtil.parseObj(forObject);
        if ((boolean) entries.get("success")) {
            JSONObject value = (JSONObject) entries.get("value");
            JSONArray jsonArray = (JSONArray) value.get("list");
            if (jsonArray.isEmpty()) {
                throw new RuntimeException("采集完成");
            }
            for (Object obj : jsonArray) {
                JSONObject jsonObject = (JSONObject) obj;
                String hm = (String) jsonObject.get("lotteryDrawResult");
                String rq = (String) jsonObject.get("lotteryDrawTime");
                String str = rq.replace("-", "") + "-" + hm.replace(" ", "");
                list.add(str);
            }
        }
        return list;
    }

}
